#include "HeadshotLabel.h"

#include <CommonUtils.h>
#include <DownloadFile.h>

HeadshotLabel::HeadshotLabel(QWidget *parent) : QClickLabel(parent) {}

HeadshotLabel::~HeadshotLabel() {}

void HeadshotLabel::setHeadshot(const QString &_headshot) {
  this->headshot = _headshot;

  if (QPixmap(headshot).isNull()) {
    headshot = ":/resource/img/mainwindow/default.png";
  }

  if (_headshot.startsWith("http")) {
    DownloadFile *down = new DownloadFile();

    connect(down, &DownloadFile::finished,
            [=](std::shared_ptr<QByteArray> data) {
              QPixmap pixmap;
              pixmap.loadFromData(*data);
              setPixmap(CommonUtils::getRoundImage(pixmap));
              down->deleteLater();
            });

    down->download(_headshot);
  } else {
    setPixmap(CommonUtils::getRoundImage(headshot));
  }
}

QString HeadshotLabel::getHeadshot() { return headshot; }
